package com.cn.yugan.Activity;


import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.cn.yugan.mobile.R;


/**
 * Created by Rover on 2016/7/17 .
 * Email 357319517@qq.com
 * 重置成功-------->本活动结束
 * 重置失败-------->提示错误(留在本页)
 */
public class ChapasswordActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tv_account;
    private EditText et_chapwd, et_rechapwd;
    private Button bt_submit;
    private String account = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chapassword);
       
        
        init();
    }

    private void init() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("重置密码");
        actionBar.setDisplayHomeAsUpEnabled(true);
        
        tv_account = (TextView) findViewById(R.id.cha_username);
        bt_submit = (Button) findViewById(R.id.cha_submit);
        et_chapwd = (EditText) findViewById(R.id.cha_password);
        et_rechapwd = (EditText) findViewById(R.id.cha_repassword);
        
        et_chapwd.setImeOptions(EditorInfo.IME_ACTION_NEXT);
        et_rechapwd.setImeOptions(EditorInfo.IME_ACTION_NONE);

        et_chapwd.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    et_rechapwd.requestFocus();
                    return true;
                }
                return false;
            }
        });
        et_rechapwd.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    return true;
                }
                return false;
            }
        });
        bt_submit.setOnClickListener(this);
        Intent intent = getIntent();
        account = intent.getStringExtra("account");
        if (account != null)
            tv_account.setText(account);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
                break;
        }
        return true;
    }


    //有事件再添加
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cha_submit: {
                String pwd = et_chapwd.getText().toString();
                String repwd = et_rechapwd.getText().toString();
                //待补充修改密码逻辑    
                //根据服务器响应值做判断
                //以下为试验逻辑
                if (pwd.equals(repwd)) {
                    Toast.makeText(ChapasswordActivity.this, "修改成功！", Toast.LENGTH_SHORT).show();
                    ChapasswordActivity.this.finish();
                    break;
                }else {
                    Toast.makeText(ChapasswordActivity.this, "鱼干娘悄悄告诉你：欧尼酱！两次密码输入不一致！", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}
